package at.fhooe.obd.gui;

import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;

public class LineGraph
{
	private static final String TAG = "OBD_Client";
	XYMultipleSeriesDataset dataset = null;
	XYMultipleSeriesRenderer mRenderer = null;
	Context context = null;
	GraphicalView view = null;
//	Intent intent = null;
	
	private boolean init(List<TimeSeries> seriesToDisplay, 
			List<Integer> seriesColors, List<PointStyle> seriesStyles, String title, 
			boolean showLegend)
	{
		if (seriesToDisplay.size() != seriesColors.size() && 
				seriesToDisplay.size() != seriesStyles.size())
		{
			Log.e(TAG, "LineGraph_Cnstructor: The sizes of all given lists must match!");
			return false;
		}
		
		dataset = new XYMultipleSeriesDataset();
		mRenderer = new XYMultipleSeriesRenderer(); // Holds a collection of XYSeriesRenderer and customizes the graph
		mRenderer.setApplyBackgroundColor(true);
		mRenderer.setBackgroundColor(Color.BLACK);
//		mRenderer.setChartTitle(title);
//		mRenderer.setChartTitleTextSize(50F);
		mRenderer.setAxisTitleTextSize(50F);
		mRenderer.setLabelsTextSize(30F);
//		mRenderer.setBarSpacing(100);s
//		mRenderer.setFitLegend(true);
//		mRenderer.setLegendTextSize(40F);
		mRenderer.setShowGrid(true);
		
		mRenderer.setShowLegend(showLegend);
		
		int i = 0;
		for (TimeSeries serie : seriesToDisplay)
		{
			dataset.addSeries(serie);
			XYSeriesRenderer renderer = new XYSeriesRenderer();
			mRenderer.addSeriesRenderer(renderer);
			renderer.setColor(seriesColors.get(i));
			renderer.setLineWidth(5F);
			renderer.setPointStyle(seriesStyles.get(i));
			renderer.setDisplayChartValues(true);
			renderer.setFillPoints(true);
			
			i++;
		}
		
		return true;
	}
	
	public Intent getIntent(Context context, List<TimeSeries> seriesToDisplay, 
			List<Integer> seriesColors, List<PointStyle> seriesStyles, String title,
			boolean showLegend)
	{
		if (!init(seriesToDisplay, seriesColors, seriesStyles, title, showLegend))
		{
			return null;
		}
		
		Intent intent = ChartFactory.getLineChartIntent(context, dataset, mRenderer, title);
		
		return intent;
	}
	
	public GraphicalView getView(Context context, List<TimeSeries> seriesToDisplay, 
			List<Integer> seriesColors, List<PointStyle> seriesStyles, String title,
			boolean showLegend)
	{
		if (!init(seriesToDisplay, seriesColors, seriesStyles, title, showLegend))
		{
			return null;
		}
		
		GraphicalView view = ChartFactory.getLineChartView(context, dataset, mRenderer);
		this.view = view;
		
		return view;
	}
	
	/**
	 * Adds one new value to all series in the plot.
	 * new value must be given for each serie in the plot!
	 * 
	 * Note that the order of the values MUST be exactly the order
	 * of the series in the dataset of the plot!
	 * 
	 * @param newXValueForEachSerie the new x values for each serie
	 * @param newXValueForEachSerie the new y value for each serie
	 */
	public void updateData(int[] newXValueForEachSerie, 
			int[] newYValueForEachSerie)
	{
		if (newXValueForEachSerie.length != newYValueForEachSerie.length &&
				newXValueForEachSerie.length != dataset.getSeriesCount())
		{
			Log.e(TAG, "LineGraph_update: Nr of series values must match nr of series.");
			return;
		}
		
		for (int i = 0; i < dataset.getSeriesCount(); i++)
		{
			dataset.getSeriesAt(i).add(
					newXValueForEachSerie[i], 
					newYValueForEachSerie[i]);
		}
		
		if (this.view != null)
			view.repaint();
		
	}
}
